﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaseUI;
using IUI;
using BL;
using Data;

namespace WinUI
{
    public partial class ManagePlaylistForm : //Form
                                              BaseWinForm<ManagePlaylistController>, IManagePlaylist
    {
        public ManagePlaylistForm(Playlist list)
        {
            InitializeComponent();

            _id_playlist = list.ID;
        }

        #region Члены IManagePlaylist

        Guid _id_playlist;
        Playlist IManagePlaylist.Playlist
        {
            get { return Playlist.GetById(_id_playlist); }
        }

        Song IManagePlaylist.SongToRemove
        {
            get 
            {
                int ?k = null;

                for (int i = 0; i < lst_Current.Items.Count; i++)
                {
                    k = i;
                    if (lst_Current.GetSelected(i)) { break; }
                }

                Song tmp = null;

                if (k.HasValue)
                {
                    tmp = (Song) lst_Current.Items[k.Value];

                    lst_Current.Items.RemoveAt(k.Value);
                }

                return tmp;
            }
        }

        Guid IManagePlaylist.IdSongToAdd
        {
            get
            {
                int? k = null;

                for (int i = 0; i < lst_All.Items.Count; i++)
                {
                    k = i;
                    if (lst_All.GetSelected(i)) { break; }
                }

                Guid g = Guid.Empty;
                if (k.HasValue)
                {
                    g = new Guid((String)_allSongs.Rows[k.Value]["id"]);
                }

                return g;
            }
        }

        private DataTable _allSongs;
        DataTable IManagePlaylist.AllSongs
        {
            get
            {
                return _allSongs;
            }
        }

        void IManagePlaylist.FillLists(DataTable tbl)
        {
            if (null != tbl)
            {
                _allSongs = tbl;
            }

            lst_Current.Items.Clear();
            foreach (var song in ((IManagePlaylist)this).Playlist.Songs)
            {
                lst_Current.Items.Add((Song)song);
            }

            lst_All.Items.Clear();
            foreach (var row in _allSongs.Rows)
            {
                String item = "";
                foreach (var column in _allSongs.Columns)
                {
                    String column_name = ((DataColumn)column).ColumnName;
                    if ("id" != column_name)
                    {
                        item += (String)((DataRow)row)[column_name] + " - ";
                    }
                }

                item = item.Remove(item.Length - 3);
                lst_All.Items.Add(item);
            }
        }

        private EventHandler _shown;
        EventHandler IManagePlaylist.Shown
        {
            get
            {
                return _shown;
            }
            set
            {
                _shown = value;
            }
        }

        private EventHandler _click_Add;
        EventHandler IManagePlaylist.Click_Add
        {
            get
            {
                return _click_Add;
            }
            set
            {
                _click_Add = value;
            }
        }

        private EventHandler _click_Remove;
        EventHandler IManagePlaylist.Click_Remove
        {
            get
            {
                return _click_Remove;
            }
            set
            {
                _click_Remove = value;
            }
        }

        #endregion

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (_click_Add != null) _click_Add(this, null);
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            if (_click_Remove != null) _click_Remove(this, null);
        }

        private void ManagePlaylistForm_Shown(object sender, EventArgs e)
        {
            if (_shown != null) _shown(this, null);
        }
    }
}
